// 工程化
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)


const router = new VueRouter({
  // 路由配置
  routes: [
    // 一打开默认是首页
    {
      name: 'default',
      path: '/',
      redirect: { name: 'index' }
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
    }, {
      name: 'ArticleDetail',
      path: '/ArticleDetail/:id',
      component: () => import('@/views/ArticleDetail.vue')
    },
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue')
    },
    {
      name: 'myfollow',
      path: '/myfollow',
      component: () => import('@/views/myfollow.vue')
    }, {
      name: 'statr',
      path: '/statr',
      component: () => import('@/views/statr.vue')
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    }, {
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/personal.vue')
    }, {
      name: 'edit_profile',
      path: '/edit_profile/:id',
      component: () => import('@/views/edit_profile.vue')
    }, {
      name: 'comments',
      path: '/comments/:id',
      component: () => import('@/views/comments.vue')
    }
  ]
})


// 添加路由守卫——路由导航
// 作用是通过判定决定当前的路由跳转到底能不能进行
// 这种守卫，只要进行路由跳转就会自动的触发，不能人为调用
router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(from);
  // 判断当前用户是否登录，如果登录了则可以跳转，否则重定向到登录页
  // 获取用户登录之后的token，进行token的判断
  if (to.name == 'personal') {  //判断是否跳转到个人中心页
    let token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      // 说明没有登录
      next({ name: 'login', query: { redirect: to.fullPath } })
    }
  } else {
    next()
  }

})


// 暴露
export default router